package view;

import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.*;

/**
 * LoginPageHTML implements the HTML based login page view of the Lithub
 * website.
 *
 * Primarily used for the web-based side of the Lithub project.
 * 
 * @author Reynaldo Gonzales.
 * @version 1.0
 *
 */
class LoginPageHTML extends View {

    private Model model;
    private HttpServletRequest request;
    private HttpServletResponse response;

    /**
     * LoginPageHTML(...) is the reciever of user login page view requests and
     * also the responder to such as well.
     *
     * @param a HttpServletRequest request which holds the HTTP request
     * information of the requesting user.
     *
     * @param b HttpServletResponse response to the requesting user.
     *
     */
    LoginPageHTML(HttpServletRequest req, HttpServletResponse res) {
        request = req;
        response = res;
    }

    /**
     * invoke(modem m) is a model invoker which casts the invokation of the
     * login page model implementation to try to sign a user in. On success, the
     * user is redirected to their Lithub dashboard. On fail, the user remains
     * on the front page.
     *
     * @param a model which refers to the model that implements the login page.
     *
     * @catch generic exception.
     */
    @Override
    public void invoke(Model m) {
        LoginPage l = (LoginPage) m;

        PrintWriter out = null;
        try {
            out = response.getWriter();
            if (l.userLoggedIn) {
                response.setStatus(HttpServletResponse.SC_FOUND); // SC_FOUND = 302
                response.sendRedirect("index.jsp?model=UserPage&view=UserPageHTML&action=ShowUserPage");
            } else {
                response.setStatus(HttpServletResponse.SC_FOUND); // SC_FOUND = 302
                response.sendRedirect("index.jsp");
            }
        } catch (Exception ex) {
            Logger.getLogger(FrontPageHTML.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
